package jeecg.business.controller.keyword;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.entity.keyword.KeywordEntity;
import jeecg.business.entity.keywordtype.KeywordTypeEntity;
import jeecg.business.service.keyinfo.KeyinfoServiceI;
import jeecg.business.service.keyword.KeywordServiceI;
import jeecg.business.service.keywordtype.KeywordTypeServiceI;
import jeecg.business.util.WordHandlerUtil;
import jeecg.system.service.SystemService;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**   
 * @Title: Controller
 * @Description: 关键词
 * @author LinYingPiaoJian
 * @date 2015-06-04 17:31:49
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/keywordController")
public class KeywordController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(KeywordController.class);

	@Autowired
	KeyinfoServiceI keyinfoService;
	@Autowired
	private KeywordServiceI keywordService;
	@Autowired
	private KeywordTypeServiceI keywordTypeService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	@RequestMapping(params = "11111111")
	public ModelAndView test111111(HttpServletRequest request) {
		System.out.println("----11111111方法进来了---"+request.getRequestURL() + "?" + request.getQueryString()+"--");
		return null;
	}


    /**
     * 添加关键词
     *
     * @param ids
     * @return
     */
    @RequestMapping(params = "save")
    @ResponseBody
    public AjaxJson save(KeywordEntity keyword, HttpServletRequest request) {
        System.out.println("gogo begin###########################");
        logger.info("gogo begin###########################");
        AjaxJson j = new AjaxJson();
        System.out.println("save key begin#########");
        logger.info("save key begin#########");
        if (StringUtil.isNotEmpty(keyword.getId())) {
            message = "更新成功";
            KeywordEntity t = keywordService.get(KeywordEntity.class, keyword.getId());
            try {
                MyBeanUtils.copyBeanNotNull2Bean(keyword, t);
                keywordService.saveOrUpdate(t);
                systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("key save 保存失败",e);
            }
        } else {
            System.out.println("add key ##########3");
            logger.info("add key ##########3");
            message = "添加成功";
            keywordService.save(keyword);
            systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
        }

        /**
         * 屏蔽词
         */
        if(StringUtil.isNotEmpty(keyword.getBppgjc())){
            List<KeyinfoEntity> list = keyinfoService.list(keyword.getQyid(), "0");
            if(list != null)
                for(KeyinfoEntity k : list)
                    if(WordHandlerUtil.hasShieldWord(k.getTitle() , keyword.getBppgjc())){
                        k.setSfsc("1");
                        keyinfoService.updateEntitie(k);
                    }
        }

        System.out.println("save key end#########55");
        logger.info("save key end#########");
        return j;
    }


	/**
	 * 关键词列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "keywordlist")
	public ModelAndView keyword(HttpServletRequest request) {
        System.out.println("----keywordlist方法进来了---"+request.getRequestURL() + "?" + request.getQueryString()+"--");
		request.setAttribute("qyid", request.getParameter("qyid"));
		return new ModelAndView("jeecg/business/keyword/keywordList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(KeywordEntity keyword,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(KeywordEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, keyword);
		this.keywordService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除关键词
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(KeywordEntity keyword, HttpServletRequest request) {
		System.out.println("11111111111111111111111111111");
		logger.info("222222222222222222222222222222222222222222222222");
		AjaxJson j = new AjaxJson();
		keyword = systemService.getEntity(KeywordEntity.class, keyword.getId());
		message = "删除成功";
		keywordService.delete(keyword);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}



	/**
	 * 关键词列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(KeywordEntity keyword, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(keyword.getId())) {
			keyword = keywordService.getEntity(KeywordEntity.class, keyword.getId());
		}else{
			if(keyword == null)
				keyword = new KeywordEntity();
			keyword.setSortnum(999);
			keyword.setSsyqzqys(20);
			keyword.setXwmhzqys(5);
			keyword.setLtzqys(10);
			keyword.setBkzqys(5);
			keyword.setWxzqys(5);
			keyword.setWbzqys(10);
			keyword.setSpzqys(5);
		}
		req.setAttribute("keywordPage", keyword);
		List<KeywordTypeEntity> list = keywordTypeService.list(keyword.getQyid());
		req.setAttribute("typelist", list);
		return new ModelAndView("jeecg/business/keyword/keyword");
	}
	
	@RequestMapping(params = "toAssignKeyword")
	public ModelAndView toAssignKeyword(String id , HttpServletRequest req){
		req.setAttribute("enterid",id);
		String [] selectids = keywordService.getSelectedId(id);		
		req.setAttribute("selectids", ","+getSelectedIds(selectids)+",");
		return new ModelAndView("jeecg/business/keyword/assignkeywordList");
	}
	
	String getSelectedIds(String[] selectids){
		String subjectids = "";
		if(selectids != null && selectids.length > 0){
			for(int i=0;i<selectids.length;i++){
				if(subjectids.length() == 0) subjectids = selectids[i];
				else subjectids += ","+selectids[i];
			}
		}
		return subjectids;
	}
	
	/**
	 * 添加课程
	 * 
	 * @return
	 */
	@RequestMapping(params = "assignKeyword")
	@ResponseBody
	public AjaxJson assignKeyword(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String enterid = request.getParameter("enterid");		
		String ids = request.getParameter("ids");	
		keywordService.assignKeyword(enterid, ids);
		message = "分配成功";	
		j.setMsg(message);
		String [] selectids = keywordService.getSelectedId(enterid);	
		j.setObj(","+getSelectedIds(selectids)+",");
		return j;
	}
	
	/**
	 * 删除课程
	 * 
	 * @return
	 */
	@RequestMapping(params = "delKeyword")
	@ResponseBody
	public AjaxJson delKeyword(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String enterid = request.getParameter("enterid");		
		String ids = request.getParameter("ids");	
		keywordService.delKeyword(enterid, ids);
		message = "解除成功";	
		j.setMsg(message);
		String [] selectids = keywordService.getSelectedId(enterid);
		j.setObj(","+getSelectedIds(selectids)+",");
		return j;
	}
	
	/**
	 * 删除课程
	 * 
	 * @return
	 */
	@RequestMapping(params = "getKeywordHtml")
	@ResponseBody
	public AjaxJson getKeywordHtml(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String qyid = getEnter().getId();
		String lx = request.getParameter("lx");
		List<KeywordEntity> klist = keywordService.find(qyid, lx , null);
		String str = "<option value=''>----全部----</option>";
		for(KeywordEntity k : klist){
			str += "<option '"+k.getKeyword()+"'>"+k.getKeyword()+"</option>";
		}
		j.setMsg(str);
		return j;
	}
}
